package study.javaee.practice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import study.javaee.practice.common.annotation.CreateValidate;
import study.javaee.practice.common.annotation.UpdateValidate;
import study.javaee.practice.common.api.CommonResult;
import study.javaee.practice.entity.User;
import study.javaee.practice.service.UserService;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @ApiOperation("获取用户数据")
    @GetMapping
    @PreAuthorize( "hasRole('ROLE_ADMIN')")
    public CommonResult<IPage<User>> getUsers(@Length(max = 10) String name, @Range(min = 1, max = 3) Integer type, Integer page, @Size(min = 1, max = 200) Integer pageSize){
        User user = new User();
        user.setName(name);
        user.setType(type);
        IPage<User> userIPage = userService.getUsers(user, page, pageSize);
        return CommonResult.success(userIPage);
    }

    @ApiOperation("增加用户")
    @PostMapping
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public CommonResult<Object> addUser(@Validated(CreateValidate.class) User user){
        userService.addUser(user);
        return CommonResult.success(null);
    }

    @ApiOperation("修改用户")
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public CommonResult<Object> updateUser(@Validated(UpdateValidate.class) User user,
                                           @PathVariable @Min(1) int id){
        userService.updateUserById(user, id);
        return CommonResult.success(null, "修改成功");
    }


    @ApiOperation("获取单个用户数据")
    @GetMapping("/{id}")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public CommonResult<User> getUser(@Size(min = 1) @NotNull @PathVariable Integer id){
        User user = userService.getUserById(id);
        user.setPassword(null);
        return CommonResult.success(user);
    }

    @ApiOperation("修改用户个人数据")
    @PutMapping("/profile")
    @PreAuthorize("!isAnonymous()") // 确保当前有用户登录
    public CommonResult<Object> updateProfile(@Validated(UpdateValidate.class) User user){
        userService.updateCurrentUser(user);
        return CommonResult.success(null, "修改个人信息成功");
    }

    @ApiOperation("获取当前用户数据")
    @GetMapping("/profile")
    @PreAuthorize("!isAnonymous()")
    public CommonResult<User> getProfile(){
        User user = userService.getCurrentUser(true);
        assert user!=null;
        user.setPassword(null);
        return CommonResult.success(user);
    }

}
